<?php

namespace app\data\model;

use think\admin\Model;
use think\helper\Str;

class ActOrder extends Model
{
    protected $table = 'act_order';
    protected $autoTimestamp = false;

    const DEFAULT_PAY_TYPE = 1;
    //状态
    const STATUS_CREATED = 1;
    const STATUS_PAYED = 2;
    const STATUS_REFUNDED = 3;
    const STATUS =[
        self::STATUS_CREATED=>'已创建',
        self::STATUS_PAYED=>'已支付',
        self::STATUS_REFUNDED=>'已退款',
    ];

    public static function onBeforeInsert(ActOrder $model)
    {
        $model->setAttr('sn','T'.Str::random(15).time().mt_rand(1000,9999));
    }

    public function getTicketInfoAttr()
    {
        return ActActivityTicket::where('id',$this->ticket_id)->find() ?? [];
    }

    public function getActivityInfoAttr()
    {
        return ActActivity::where('id',$this->activity_id)->find() ?? [];
    }

    public function getStatusNameAttr($value,$data)
    {
        $status = [1=>'已创建',2=>'已支付',3=>'待审核',4=>'已出票',5=>'已退款',6=>'无效'];
        return $status[$data['status']];
    }

    public function user(){
        return $this->belongsTo(ActUserModel::class,'buy_user_id')->field('id,name,email,phone');
    }

    public function activityModel()
    {
        return $this->belongsTo(ActActivity::class,'activity_id');
    }

    public function diy(){
        return $this->hasMany(ActActivityDiy::class,'activity_id');
    }

    public function getDiyDataAttr($value,$data){
        return $data['diys'] ? json_decode($data['diys'],true) : [];
    }
 
    public function getUpdateTimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }
}